package com.cms.service.table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.cms.dbUtils.GenEntityMysql;
import com.cms.vo.TableBeanVo;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;


public class TableServiceImpl implements TableService {

	/**
	 * 查询所有数据库表
	 */
	@Override
	public Map<String, Object> getTableALL(int pageNumber,int pageSize,String search,String table_name ) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Page<Record> recordList = null;
		StringBuilder querySQL = new StringBuilder("from information_schema.tables where table_schema= 'cms'");
		if (null != search) {
			querySQL.append(" and table_name like ").append("'%" + search + "%'");
		}
		if(null !=table_name){
			querySQL.append(" and table_name like ").append("'%" + table_name + "%'");
		}
			recordList = Db.paginate(pageNumber, pageSize, "select table_name ",
					querySQL.toString());
		returnMap.put("total", recordList.getTotalRow());
		returnMap.put("rows", recordList.getList());
		return returnMap;
	}

	@Override
	public void createTable(String tableLists) {
		List<String> lists = new ArrayList<String>();
		 List<TableBeanVo> tableList = JSONObject.parseArray(tableLists, TableBeanVo.class); 
  	     if(!CollectionUtil.isEmpty(tableList)){
  		   for (TableBeanVo tableBeanVo : tableList) {
  			 lists.add(tableBeanVo.getTable_name());
  		   }
  	   }
  	   //生成实体类代码
	  new GenEntityMysql(lists);
	}

	

}
